package ai.people.netmon.telegram.service.impl;

import ai.people.netmon.framework.domain.telegram.entity.Channels;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChannelsView;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.telegram.domain.request.SearchAccountChannelRequest;
import ai.people.netmon.telegram.domain.request.channels.ChannelsRequest;
import ai.people.netmon.telegram.mapper.ChannelsMapper;
import ai.people.netmon.telegram.mapstruct.ChannelsMapStruct;
import ai.people.netmon.telegram.service.ChannelsService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author yuanqinglong
 * @date 2022/1/10 10:02
 */
@Service
public class ChannelsServiceImpl implements ChannelsService {

    @Autowired
    private ChannelsMapper channelsMapper;


    @Autowired
    private ChannelsMapStruct channelsMapStruct;

    /**
     * 分页查询群组
     *
     * @param channelsRequest 查询条件
     * @return {@link PageResponse}<{@link Channels}>
     */
    @Override
    public PageResponse<ChannelsView> businessChannelsList(ChannelsRequest channelsRequest) {
        //Page<Channels> page = new Page<>(channelsRequest.getPageNo(), channelsRequest.getPageSize());
        //LambdaQueryWrapper<Channels> lambdaQuery = Wrappers.lambdaQuery();
        //lambdaQuery.select(Channels::getGroupname, Channels::getImportanceDeg,
        //        Channels::getId, Channels::getBusinessType, Channels::getMembers);
        //Page<Channels> channelsPage = channelsMapper.selectPage(page, lambdaQuery);
        //List<ChannelsView> channelsViews = channelsPage.getRecords().stream().map(channels -> channelsMapStruct.channelsToChannelsView(channels)).collect(Collectors.toList());
        long channelsCount = channelsMapper.businessChannelsCount(channelsRequest);
        if (channelsCount == 0){
            return PageResponse.builderEmpty();
        }
        List<ChannelsView> channelsViewList = channelsMapper.businessChannelsList(channelsRequest);
        // TODO: 2022/1/27 加入分析标识

        return new PageResponse<>(channelsViewList, channelsCount);
    }


    /**
     * 搜索群组
     *
     * @param searchChannelRequest 搜索群组请求
     * @return {@link PageResponse}<{@link ChannelsView}>
     */
    @Override
    public PageResponse<ChannelsView> searchChannels(SearchAccountChannelRequest searchChannelRequest) {
        List<String> fields = Optional.ofNullable(Strings.emptyToNull(searchChannelRequest.getSelectFields())).map(s -> Arrays.asList(s.split(","))).orElse(Collections.emptyList());
        long searchChannelCount = channelsMapper.searchChannelCount(searchChannelRequest.getSearchKeywords(), fields);
        if (searchChannelCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<Channels> searchChannelList = channelsMapper.searchChannelList(searchChannelRequest.getPageNo(), searchChannelRequest.getPageSize(), searchChannelRequest.getSearchKeywords(), fields);
        List<ChannelsView> channelsViewList = searchChannelList.stream().map(channels -> channelsMapStruct.channelsToChannelsView(channels)).collect(Collectors.toList());
        // TODO: 2022/1/24 加入分析标识
        return PageResponse.builder(channelsViewList, searchChannelCount);
    }

}
